package com.zenith.middleware.controller;

import com.zenith.common.result.PageResult;
import com.zenith.common.result.Result;
import com.zenith.middleware.po.dto.storage.*;
import com.zenith.middleware.po.vo.storage.*;
import com.zenith.middleware.service.StorageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 带缓存功能的存储控制器
 */
@Slf4j
@RestController
@RequestMapping("/middleware/storage")
@Tag(name = "存储管理", description = "存储管理接口")
@RequiredArgsConstructor
public class StorageController {
    
    private final StorageService storageService;
    
    // =========================== 分片上传相关接口 ===========================
    
    @Operation(summary = "初始化分片上传")
    @PostMapping("/initChunkUpload")
    public Result<ChunkInitVO> initChunkUpload(@Valid @RequestBody ChunkInitDTO chunkInitDTO) {
        return storageService.initChunkUpload(chunkInitDTO);
    }

    /**
     * 上传分片
     */
    @Operation(summary = "上传分片")
    @PostMapping("/chunkUploadService")
    public Result<ChunkUploadVO> uploadChunk(@Valid @RequestBody ChunkUploadDTO chunkUploadDTO) {
        return storageService.uploadChunk(chunkUploadDTO);
    }

    /**
     * 完成分片上传
     */
    @Operation(summary = "完成分片上传")
    @PostMapping("/completeChunkUpload")
    public Result<FileVO> completeChunkUpload(@Valid @RequestBody ChunkCompleteDTO chunkCompleteDTO) {
        return storageService.completeChunkUpload(chunkCompleteDTO);
    }

    /**
     * 获取分片上传状态
     */
    @Operation(summary = "获取分片上传状态")
    @PostMapping("/getChunkStatus/{uploadKey}")
    public Result<ChunkStatusVO> getChunkStatus(@PathVariable String uploadKey) {
        return storageService.getChunkStatus(uploadKey);
    }

    // =========================== 网盘功能相关接口 ===========================
    
    @Operation(summary = "创建文件夹")
    @PostMapping("/createFolder")
    public Result<FolderVO> createFolder(@Valid @RequestBody FolderDTO folderDTO) {
        return storageService.createFolder(folderDTO);
    }

    /**
     * 获取文件夹树
     */
    @Operation(summary = "获取文件夹树")
    @PostMapping("/getFolderTree")
    public Result<List<FolderVO>> getFolderTree(@RequestParam Long userId) {
        return storageService.getFolderTree(userId);
    }

    /**
     * 获取文件夹中的文件列表
     */
    @Operation(summary = "获取文件夹中的文件列表")
    @PostMapping("/getFolderFiles")
    public Result<PageResult<FolderFileVO>> getFolderFiles(@RequestBody FolderFileQueryDTO queryDTO) {
        return storageService.getFolderFiles(queryDTO);
    }

    /**
     * 移动文件
     */
    @Operation(summary = "移动文件")
    @PostMapping("/moveFile")
    public Result<Boolean> moveFile(@Valid @RequestBody MoveFileDTO moveFileDTO) {
        return storageService.moveFile(moveFileDTO);
    }

    /**
     * 重命名文件或文件夹
     */
    @Operation(summary = "重命名文件或文件夹")
    @PostMapping("/rename")
    public Result<Boolean> rename(@Valid @RequestBody RenameDTO renameDTO) {
        return storageService.rename(renameDTO);
    }

    // =========================== 文件管理相关接口 ===========================
    
    @Operation(summary = "初始化上传")
    @PostMapping("/initUpload")
    public Result<UploadInitVO> initUpload(@Valid @RequestBody UploadInitDTO uploadInitDTO) {
        return storageService.initUpload(uploadInitDTO);
    }

    /**
     * 上传文件
     */
    @Operation(summary = "上传文件")
    @PostMapping("/uploadFile")
    public Result<FileVO> uploadFile(@RequestParam("file") MultipartFile file,
                                     @RequestParam("fileId") Long fileId) {
        return storageService.uploadFile(file, fileId);
    }

    /**
     * 下载文件
     */
    @Operation(summary = "下载文件")
    @PostMapping("/downloadFile/{fileId}")
    public void downloadFile(@PathVariable Long fileId, HttpServletResponse response) {
        storageService.downloadFile(fileId, response);
    }

    /**
     * 删除文件
     */
    @Operation(summary = "删除文件")
    @PostMapping("/deleteFile/{fileId}")
    public Result<Boolean> deleteFile(@PathVariable Long fileId) {
        return storageService.deleteFile(fileId);
    }

    /**
     * 获取文件详情
     */
    @Operation(summary = "获取文件详情")
    @PostMapping("/getFileDetail/{fileId}")
    public Result<FileVO> getFileDetail(@PathVariable Long fileId) {
        return storageService.getFileDetail(fileId);
    }

    /**
     * 分页查询文件列表
     */
    @Operation(summary = "分页查询文件列表")
    @PostMapping("/listFiles")
    public Result<PageResult<FileVO>> listFiles(@RequestBody FileQueryDTO queryDTO) {
        return storageService.listFiles(queryDTO);
    }

    // =========================== 存储提供商相关接口 ===========================
    
    /**
     * 获取所有存储提供商列表
     */
    @Operation(summary = "获取所有存储提供商列表")
    @PostMapping("/listProviders")
    public Result<List<StorageProviderVO>> listProviders() {
        return storageService.listProviders();
    }

    /**
     * 添加存储提供商
     */
    @Operation(summary = "添加存储提供商")
    @PostMapping("/addProvider")
    public Result<Boolean> addProvider(@Valid @RequestBody ProviderDTO providerDTO) {
        return storageService.addProvider(providerDTO);
    }

    /**
     * 更新存储提供商
     */
    @Operation(summary = "更新存储提供商")
    @PostMapping("/updateProvider")
    public Result<Boolean> updateProvider(@Valid @RequestBody ProviderDTO providerDTO) {
        return storageService.updateProvider(providerDTO);
    }

    /**
     * 删除存储提供商
     */
    @Operation(summary = "删除存储提供商")
    @PostMapping("/deleteProvider/{id}")
    public Result<Boolean> deleteProvider(@PathVariable Long id) {
        return storageService.deleteProvider(id);
    }

    /**
     * 设置默认存储提供商
     */
    @Operation(summary = "设置默认存储提供商")
    @PostMapping("/setDefaultProvider/{id}")
    public Result<Boolean> setDefaultProvider(@PathVariable Long id) {
        return storageService.setDefaultProvider(id);
    }

    /**
     * 获取默认存储提供商
     */
    @Operation(summary = "获取默认存储提供商")
    @PostMapping("/getDefaultProvider")
    public Result<StorageProviderVO> getDefaultProvider() {
        return storageService.getDefaultProvider();
    }
}